#!/bin/bash
apt install openjdk-8-jdk
java -version

wget -nc https://mirrors.aliyun.com/apache/hive/hive-4.0.1/apache-hive-4.0.1-bin.tar.gz
tar -zxvf apache-hive-4.0.1-bin.tar.gz
rm -rf /usr/local/hive
mkdir /usr/local/hive
mv apache-hive-4.0.1-bin/* /usr/local/hive

wget -nc https://repo1.maven.org/maven2/org/postgresql/postgresql/42.7.4/postgresql-42.7.4.jar
mv postgresql-42.7.4.jar /usr/local/hive/lib

tee /usr/local/hive/conf/hive-site.xml <<-'EOF'
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:postgresql://172.19.109.227:5432/metastore_db</value>
    <description>JDBC connect string for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>org.postgresql.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>metastore_db</value>
    <description>username to use against metastore database</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>65Bh5ekPiZT8aRGT</value>
    <description>password to use against metastore database</description>
  </property>
  
  <property>
    <name>hive.server2.enable.doAs</name>
    <value>false</value>
    <description>Setting this property to true will have HiveServer2 execute Hive operations as the user making the calls to it.</description>
  </property>
</configuration>

EOF

schematool -dbType postgres -initSchema